/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */

package cn.webrx;

/**
 * <p>Project: jse2303 - Ticket</p>
 * <p>Powered by webrx On 2023-07-26 15:22:04</p>
 * <p>描述：<p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ticket implements Runnable {
    private int num = 100;

    public Ticket(int num) {
        this.num = num;
    }

    public Ticket() {
    }

    @Override
    public void run() {
        String t = Thread.currentThread().getName();
        System.out.printf("%s:线程启动..%n", t);
        while (true) {
            synchronized (this) {
                if (num < 1) break;
                System.out.printf("%s:售出一张票(座号:%d)，剩余%d张票%n", t, num, --num);
                try {
                    Thread.sleep(1500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Thread.yield();
        }

        System.out.printf("%s:线程结束（票卖完了）%n", t);
    }
}
